package com.jinzhi.sms.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.ValidUtils;
import com.jinzhi.jzapi.utils.RedisUtil;
import com.jinzhi.jzweb.domain.MessageplatfromDO;
import com.jinzhi.jzweb.service.MessageplatfromService;
import com.jinzhi.jzweb.utils.FileUtil;
import com.jinzhi.sms.sender.AlySender;
import com.jinzhi.sms.support.SmsManager;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 *
 * </pre>
 * <small> 2018/8/30 19:17 | Aron</small>
 */
@RestController
@RequestMapping("/api/sms")
public class SmsController {

    @Autowired
    private SmsManager smsService;

    @Autowired
    private AlySender alySender;

    @Autowired
    private MessageplatfromService messageplatfromService;
    @GetMapping("/sendCode")
    @Log("发送验证码")
    @ApiOperation("发送验证码")
    public Result sendCode(String prefix, String mobile) {
        Object smsDemo = EhCacheUtil.get("登录短信验证");
        if(smsDemo == null){
            MessageplatfromDO messageplatfromDO = messageplatfromService.findOneByKv("登录短信验证");
            if(messageplatfromDO != null){
                EhCacheUtil.put("登录短信验证",messageplatfromDO.getSmsDemo());
                smsService.send(prefix, mobile,messageplatfromDO.getSmsDemo());
            }else{
                return Result.fail("短信模板错误，请联系管理员！");
            }
        }else{
            smsService.send(prefix, mobile,(String) smsDemo);
        }

        return Result.ok("发送成功");
    }


    /**
     * api小程序端-发送注册验证短信
     *
     * @Description: 调取短信平台发送短信，参数： 微信端口id，手机号码
     * @Param: [wxId, mobile]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-06-20
     */
    @PostMapping("/sendSms")
    @ApiOperation(value = "api小程序端-发送注册验证短信", httpMethod = "POST")
    public ApiResult<?> sendSms( String mobile) {
        //第一步验证手机号格式
        if (!ValidUtils.isMobile(mobile)) {
            return ApiResult.format("mobile");
        }
        EntityWrapper<MessageplatfromDO> wrapper=new EntityWrapper();
        wrapper.like("smsDemo","SMS_186380670");

        List<MessageplatfromDO>   messageplatfromDO =   messageplatfromService.selectList(wrapper);

        Object redis= RedisUtil.get(mobile);

        if(redis!=null){
            return ApiResult.fail("发送频繁，请休息一下！");
        }
        if(messageplatfromDO != null&&messageplatfromDO.size()>0){
            alySender.send(mobile ,messageplatfromDO.get(0).getSmsDemo());
        }else{
            return ApiResult.fail("短信模板错误，请联系管理员！");
        }
        return ApiResult.success("发送成功");
    }




//    @GetMapping("/sendSms")
//    @Log("发送短信")
//    @ApiOperation("发送短信")
//    @RequiresAuthentication
//    public Result sendSms(String mobile,String templateCode) {
//        Map map = new HashMap();
//        map.put("msg","测试服务");
//        smsService.send(mobile, templateCode, map);
//        return Result.ok("发送成功");
//    }

    @GetMapping("/verifyCode")
    @Log("校验验证码")
    @ApiOperation("校验验证码")
    public Result verifyCode(String prefix, String mobile, String code) {
        smsService.verify(prefix, mobile, code);
        return Result.ok("校验成功");
    }

    @GetMapping("/delFile")
    @Log("删除文件")
    @ApiOperation("删除文件")
    public Result delFile(String filePath) {
        FileUtil.delFile(filePath);
        return Result.ok("删除成功");
    }



}
